<?php

class CommentController extends AdminController
{
	public function init() {
		parent::init();
		$h = new CTextHighlighter();
		$h->registerClientScript();	
	}
	
	public function actionIndex($bookFlag)
	{
		$book = $this->getBookInfo($bookFlag);
		$comments = Yii::app()->db->createCommand()->from('{{module_'.$book->moduleId.'_comments}}')
								->order('addTime desc')->limit(100)
								->queryAll();
		
		$this->navText = '书籍: '.$book->bookName.': 评论管理';
		
		$this->render('index', array(
				'comments'=>$comments,
				'searchModel'=> new CommentSearchForm(),
				'book'=>$book,
		));
	}
	
	public function actionSearch($bookFlag) {
		
		if (isset($_POST['CommentSearchForm']))
		{
			$book = $this->getBookInfo($bookFlag);
			$comments = false;
			$searchModel = new CommentSearchForm();
			$searchModel->attributes = $_POST['CommentSearchForm'];
			if ($searchModel->validate())
			{
				$params = array(
						':name'=>'%'.$searchModel->name.'%',
						':clientIp'=>'%'.$searchModel->clientIp.'%',
						':email'=>'%'.$searchModel->email.'%',
						':comment'=>'%'.$searchModel->comment.'%',
				);
				$where = 'name like :name AND clientIp like :clientIp AND email like :email AND comment like :comment';
				if (intval($searchModel->sectionId)!=0) {
					$where .= ' AND sectionId=:sectionId';
					$params[':sectionId'] = $searchModel->sectionId;
				}
				if (intval($searchModel->chapterId)!=0) {
					$where .= ' AND chapterId=:chapterId';
					$params[':chapterId'] = $searchModel->chapterId;
				}
				$comments = Yii::app()->db->createCommand()->from('{{module_'.$book->moduleId.'_comments}}')
					->where($where)
					->order('addTime desc')
					->queryAll(true, $params);
			}
			
			$this->navText = '书籍: '.$book->bookName.': 评论管理: 搜索';
			$this->render('index', array(
					'comments'=>$comments,
					'searchModel'=> $searchModel,
					'book'=>$book,
			));
		}
		else {
			$this->redirect(array('index'));
		}
	}
	
	/**
	 * 翻译
	 * @param unknown_type $bookFlag
	 * @param unknown_type $commentId
	 */
	public function actionTranslate($bookFlag, $commentId)
	{
		$book = $this->getBookInfo($bookFlag);
		Comment::setTableId($book->moduleId);
		$comment = Comment::model()->findByPk($commentId);
		$chapterFlag = $this->getChapterFlag($book->moduleId, $comment->chapterId);	
		$sectionId = $comment->sectionId;
		
		$this->redirect(array('section/translate', 'bookFlag'=>$bookFlag, 'chapterFlag'=>$chapterFlag, 'sectionId'=> $sectionId));
		
	}
	
	/**
	 * reply 回复评论
	 */
	public function actionReply($bookFlag, $chapterId, $sectionId)
	{
		$this->layout=false;
		$book = $this->getBookInfo($bookFlag);
		$chapterFlag = $this->getChapterFlag($book->moduleId, $chapterId);
		Comment::setTableId($book->moduleId);
		$comment = new Comment();
		
		if (isset($_POST['Comment']))
		{
			$comment->attributes=$_POST['Comment'];
			$comment->sectionId = $sectionId;
			$comment->chapterId = $chapterId;
			$comment->addTime = time();
			$comment->clientIp = AppHelper::getClientIp();
			if (Yii::app()->user->name!='Guest') $comment->name=Yii::app()->user->name;
			
			$return = ActiveForm::validate($comment);
			if ($return=='[]') {
				$comment->save(false);
				//清除本章评论数据缓存
				Yii::app()->cache->delete($this->getCacheId('commentsCounts', $bookFlag.'_'.$chapterFlag));
				//清除显示评论
				Yii::app()->cache->delete($this->getCacheId('sectionComments', $bookFlag.'-'.$chapterFlag.'-'.$sectionId));
				//清除所有评论
				Yii::app()->cache->delete($this->getCacheId('comments', $bookFlag));
				echo 'ok';
			}
			else {
				echo $return;
			}
			exit;
		}
		else 
			$this->render('reply', array('comment'=>$comment, 'sectionId'=>$sectionId));
	}
	
	
	/**
	 * 删除评论
	 * @param unknown_type $bookFlag
	 * @param unknown_type $commentId
	 */
	public function actionDelete($bookFlag, $commentId)
	{
		$book = $this->getBookInfo($bookFlag);
		Comment::setTableId($book->moduleId);
		$comment = Comment::model()->findByPk($commentId);

		$chapterFlag = $this->getChapterFlag($book->moduleId, $comment->chapterId);
		$nodenum = $comment->sectionId;
		$comment->delete();
		
		//清除本章评论数据缓存
		Yii::app()->cache->delete($this->getCacheId('commentsCounts', $bookFlag.'_'.$chapterFlag));
		//清除显示评论
		Yii::app()->cache->delete($this->getCacheId('sectionComments', $bookFlag.'-'.$chapterFlag.'-'.$nodenum));
		//清除所有评论
		Yii::app()->cache->delete($this->getCacheId('comments', $bookFlag));
		$this->redirect(array('index', 'bookFlag'=>$bookFlag));
	}

}
